package com.bls.dao.impl;

import com.bls.dao.ActionLogDao;
import com.bls.entity.ActionLog;
import com.bls.entity.Draft;
import com.fy.toolhelper.db.BaseDaoImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ActionLogDaoImpl extends BaseDaoImpl<ActionLog> implements ActionLogDao {
    public ActionLogDaoImpl() throws Exception {
    }

    @Override
    public List<ActionLog> pageConditionActionLogList(Connection connection, ActionLog actionLogQuery, Integer pageSize, Integer current) throws SQLException {
        int start = pageSize * (current - 1);
        StringBuilder sql = new StringBuilder("SELECT * FROM bls_action_log WHERE 1=1 ");

        if (!StringUtils.isEmpty(actionLogQuery.getAction())) {
            sql.append(" and action like '%").append(actionLogQuery.getAction()).append("%'");
        }
        if (!StringUtils.isEmpty(actionLogQuery.getType())) {
            sql.append(" and name like '%").append(actionLogQuery.getType()).append("%'");
        }
        if (!StringUtils.isEmpty(actionLogQuery.getBandId())) {
            sql.append(" and band_id = ").append(actionLogQuery.getBandId());
        }

        sql.append(" ORDER BY time DESC limit ? , ?");

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        pstm.setInt(1, start);
        pstm.setInt(2, pageSize);
        ResultSet rs = pstm.executeQuery();
        List<ActionLog> actionLogList = new ArrayList<>();
        while (rs.next()) {
            ActionLog actionLog = getActionLogEntity(rs);
            actionLogList.add(actionLog);
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return actionLogList;
    }

    @Override
    public int pageConditionActionLogListTotal(Connection connection, ActionLog actionLogQuery) throws SQLException {

        StringBuilder sql = new StringBuilder("SELECT COUNT(*) as count FROM bls_action_log WHERE 1=1 ");

        if (!StringUtils.isEmpty(actionLogQuery.getAction())) {
            sql.append(" and action like '%").append(actionLogQuery.getAction()).append("%'");
        }
        if (!StringUtils.isEmpty(actionLogQuery.getType())) {
            sql.append(" and name like '%").append(actionLogQuery.getType()).append("%'");
        }
        if (!StringUtils.isEmpty(actionLogQuery.getBandId())) {
            sql.append(" and band_id = ").append(actionLogQuery.getBandId());
        }
        sql.append(" ORDER BY time DESC ");

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        ResultSet rs = pstm.executeQuery();
        int count = -1;
        while (rs.next()) {
            count = rs.getInt("count");
        }
        System.out.println(sql.toString());
        System.out.println(count);
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return count;
    }

    private ActionLog getActionLogEntity(ResultSet rs) throws SQLException {
        ActionLog actionLog = new ActionLog();
        actionLog.setId(rs.getLong("id"));
        actionLog.setUserId(rs.getLong("user_id"));
        actionLog.setUserName(rs.getString("user_name"));
        actionLog.setAction(rs.getString("action"));
        actionLog.setObject(rs.getString("object"));
        actionLog.setType(rs.getString("type"));
        actionLog.setBandId(rs.getLong("band_id"));
        actionLog.setBandName(rs.getString("band_name"));
        actionLog.setTime(rs.getTimestamp("time"));
        return actionLog;
    }
}
